﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 获取卷宗类型
    /// </summary>
   public class SYS_CaseType_DocType_LogicParagDAL
    {
        private string DbName;
        public SYS_CaseType_DocType_LogicParagDAL()
        {
            DbName = "JudgeDoc";
        }


        /// <summary>
        /// 获取逻辑段
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseTypeDocType(string CD_ID)
        {
            string sql =string.Format(@" select CDL_Name as Name,CDL_Code as ID from SYS_CaseType_DocType_LogicParag where CD_ID='{0}' ",CD_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取逻辑段
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseTypeDocTypeLogicParag()
        {
            string sql = " select CDL_ID as ID,CDL_Name as Name,CDL_Code as Code,CDL_ParentID as ParentID,CD_ID,CDL_Level from SYS_CaseType_DocType_LogicParag ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取逻辑分段
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseTypeDocType(string CD_Code,string CDL_Level)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
               select * from [SYS_CaseType_DocType_LogicParag] where 1 = 1 ");
            //第一级查询CD_ID
            if (CDL_Level == "1")
            {
                strSql.Append(string.Format(@" and [CDL_Level] = '{0}' and CD_Code = '{1}'", CDL_Level, CD_Code));
            }
            else //第二级及以下节点查询parentId
            {
                strSql.Append(string.Format(@" and [CDL_Level] = '{0}' and CDL_ParentID = '{1}'", CDL_Level, CD_Code));
            }

            strSql.Append(" order by CDL_ShowIndex asc ");

            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc");
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            DataTable otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            else
            {
                return new DataTable();
            }
        }
    }
}
